/*
 * $Id: docklet.c 481 2008-05-13 02:20:29Z jasta $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include <stdlib.h>

#include <gtk/gtk.h>

#include "eggtrayicon.h"

#include "config.h"

#include "../../main.h"

/*****************************************************************************/

static EggTrayIcon *docklet = NULL;
static GtkWidget *image = NULL;
static GtkWidget *menu = NULL;
static int docklet_height = -1;

/*****************************************************************************/

static const char *menu_description =
"<ui>\n"
	"<popup name='TrayMenu'>\n"
		"<menuitem action='Preferences' />\n"
		"<menuitem action='About' />\n"
		"<separator />\n"
		"<menuitem action='Quit' />\n"
	"</popup>\n"
"</ui>\n";

static void menu_about_cb(GtkAction *action, gpointer udata);
static void menu_quit_cb(GtkAction *action, gpointer udata);
static void menu_configure_cb(GtkAction *action, gpointer udata);

static GtkActionEntry menu_entries[] =
{
	{ "Preferences", GTK_STOCK_PREFERENCES, N_("Prefere_nces..."), NULL, NULL, G_CALLBACK(menu_configure_cb) },
	{ "About", GTK_STOCK_ABOUT, N_("_About"), NULL, NULL, G_CALLBACK(menu_about_cb) },
	{ "Quit", GTK_STOCK_QUIT, N_("_Quit"), NULL, NULL, G_CALLBACK(menu_quit_cb) },
};

/*****************************************************************************/

static void destroyed_cb(GtkWidget *widget, gpointer udata)
{
	g_object_unref(G_OBJECT(docklet));
	docklet = NULL;
}

static void resize_icon(GtkWidget *widget, gpointer udata)
{
	int h;

	h = MIN(widget->allocation.height, widget->allocation.width);
	printf("resize_icon(): h=%d\n", h);

	if (docklet_height == h)
		return;

	docklet_height = h;

	/* TODO: Actually react to `h'. */
	gtk_image_set_from_file(GTK_IMAGE(image),
	  DATADIR "/icons/hicolor/22x22/apps/five-server.png");
}

static void clicked_cb(GtkWidget *widget, GdkEventButton *event, gpointer udata)
{
	if (event->button == 3)
	{
		gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
		  event->button, event->time);
	}
}

static void menu_quit_cb(GtkAction *action, gpointer udata)
{
	gtk_main_quit();
}

static void menu_about_cb(GtkAction *action, gpointer udata)
{
	const gchar *authors[] = {
		"Josh Guilfoyle <jasta at devtcg org>",
	};

	const gchar *license =
	  "Five is free software; you can redistribute it and/or modify "
	  "it under the terms of the GNU General Public License as published by "
	  "the Free Software Foundation; either version 2 of the License, or "
	  "(at your option) any later version.\n\n"
	  "Five is distributed in the hope that it will be useful, "
	  "but WITHOUT ANY WARRANTY; without even the implied warranty of "
	  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
	  "GNU General Public License for more details.\n\n"
	  "You should have received a copy of the GNU General Public License "
	  "along with Five; if not, write to the Free Software Foundation, Inc., "
	  "59 Temple Place, Suite 330, Boston, MA  02111-1307  USA";

	gtk_show_about_dialog(NULL,
	  "version", VERSION,
	  "comments", "This is the server component to the Five media distribution system.  When running, media found on this computer will be available to other configured devices, such as another desktop PC or a mobile phone.",
	  "copyright", "Copyright \xC2\xA9 2008 Josh Guilfoyle",
	  "license", license,
	  "wrap-license", TRUE,
	  "authors", authors,
	  "logo-icon-name", "five-server",
	  "website", "http://five.googlecode.com",
	  "website-label", "Five Media Distribution System",
	  NULL);
}

static void menu_configure_cb(GtkAction *action, gpointer udata)
{
	GtkWidget *dialog;

	dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING,
	  GTK_BUTTONS_OK,
	  "This feature is not implemented.  You will have to use gconf-editor for now.");

	gtk_widget_show_all(dialog);

	g_signal_connect_swapped(G_OBJECT(dialog), "response",
	  G_CALLBACK(gtk_widget_destroy), dialog);
}

static void make_docklet_menu(void)
{
	GtkActionGroup *actgp;
	GtkUIManager *uimgr;
	GtkAccelGroup *acclgp;
	GError *err = NULL;

	actgp = gtk_action_group_new("TrayActions");
#ifdef ENABLE_NLS
	gtk_action_group_set_translation_domain(actgp, GETTEXT_PACKAGE);
#endif /* ENABLE_NLS */
	gtk_action_group_add_actions(actgp, menu_entries, G_N_ELEMENTS(menu_entries), NULL);

	uimgr = gtk_ui_manager_new();
	gtk_ui_manager_insert_action_group(uimgr, actgp, 0);

	if (gtk_ui_manager_add_ui_from_string(uimgr, menu_description, -1, &err) == 0)
	{
		g_error("gtk_ui_manager_add_ui_from_string() failed: %s",
		  err->message);
		g_error_free(err);
		return;
	}

	menu = gtk_ui_manager_get_widget(uimgr, "/TrayMenu");

	gtk_widget_show_all(menu);
}

static void make_docklet(void)
{
	GtkWidget *box;

	docklet = egg_tray_icon_new("Test");
	box = gtk_event_box_new();
	image = gtk_image_new();

	make_docklet_menu();

#if 0
	g_signal_connect(G_OBJECT(docklet), "embedded", G_CALLBACK(embedded_cb), NULL);
#endif
	g_signal_connect(G_OBJECT(docklet), "destroy", G_CALLBACK(destroyed_cb), NULL);
	g_signal_connect(G_OBJECT(docklet), "size-allocate", G_CALLBACK(resize_icon), NULL);
	g_signal_connect(G_OBJECT(box), "button-press-event", G_CALLBACK(clicked_cb), NULL);

	gtk_container_add(GTK_CONTAINER(box), image);
	gtk_container_add(GTK_CONTAINER(docklet), box);

	gtk_widget_show_all(GTK_WIDGET(docklet));

	g_object_ref(G_OBJECT(docklet));
}

void docklet_create_and_show(void)
{
	make_docklet();
}
